package br.com.mkmts.business.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import br.com.mkmts.business.CategoriaBO;
import br.com.mkmts.business.exception.RegistroJaCadastradoException;
import br.com.mkmts.dao.cadastro.CategoriaDao;
import br.com.mkmts.dto.CategoriaDTO;
import br.com.mkmts.utils.Adapter;
import br.com.mkmts.utils.MkmtsUtil;

@Service
public class CategoriaBOImpl implements CategoriaBO{

	@Autowired
	private CategoriaDao dao = null;

	@Override
	public List<CategoriaDTO> listarCategorias() {

		return dao.findAllCategorias();
	}
	
	@Override
	public CategoriaDTO addCategoria(CategoriaDTO dto) {
		if(getDao().existCategoria(dto)){
			throw new RegistroJaCadastradoException(dto.getDescCategoria());
		}else{
			return Adapter.entityToDto(getDao().insert(Adapter.dtoToEntity(dto)));
		}
	}
	
	@Override
	public CategoriaDTO editCategoria(CategoriaDTO dto) {
		if(getDao().existCategoria(dto)){
			throw new RegistroJaCadastradoException(dto.getDescCategoria());
		}else{
			return Adapter.entityToDto(getDao().update(Adapter.dtoToEntity(dto)));
		}
	}
	
	@Override
	public List<CategoriaDTO> filtrarCategoria(String descCategoria,
			Integer codSecao) {
		return getDao().findByFilter(
				MkmtsUtil.verificaStringNula(descCategoria)
				, MkmtsUtil.verificaIntegerNulo(codSecao));
	}
	
	@Override
	public CategoriaDTO buscarPeloCodigo(Integer cod) {
		// TODO Auto-generated method stub
		return Adapter.entityToDto(getDao().findById(cod));
	}
	
	public CategoriaDao getDao() {
		return dao;
	}

	public void setDao(CategoriaDao dao) {
		this.dao = dao;
	}
	
}
